#pragma once

#include <string>
#include <iostream>
#include <jsoncpp/json/json.h>

//#define Myself 1

const std::string num_sep = " ";
const std::string end_sep = "\n";

std::string Encode(const std::string& content)
{
    //std::cout << content << std::endl;
    std::string package = std::to_string(content.size());
    package += end_sep;
    package += content;
    package += end_sep;

    return package;
}

bool Decode(std::string& package, std::string& content)
{
    size_t pos = package.find(end_sep);
    if(pos == std::string::npos)
        return false;
    std::string len_str = package.substr(0, pos);
    int len = std::stoi(len_str);
    int all_len = len + len_str.size() + 2;
    if(all_len > package.size())
        return false;

    content = package.substr(pos + 1, len);
    package.erase(0, all_len);
    
    return true;
}


class Request
{
public:

    Request(int x, char op, int y)
        : _x(x)
        , _y(y)
        , _op(op)
    {
        
    }

    Request()
    {
        
    }

    bool serialize(std::string& content)
    {
        // "a + b" 
        // 构建报文的有效载荷
#ifdef Myself
        content = std::to_string(_x); 
        content += num_sep;
        content += _op;
        content += num_sep;
        content += std::to_string(_y);
        return true;

#else
        Json::Value root;
        root["x"] = _x;
        root["op"] = _op;
        root["y"] = _y;

        Json::FastWriter w;
        content = w.write(root);
        return true;

#endif
    }

    bool deserialize(std::string& content)
    {
        // "a + b" 
#ifdef Myself
        size_t left = content.find(num_sep);
        if(left == std::string::npos)
            return false;
        std::string p1 = content.substr(0, left);

        size_t right = content.rfind(num_sep);
        if(right == std::string::npos)
            return false;
        std::string p2 = content.substr(right + 1);

        if(left + 2 != right)
            return false;

        _x = std::stoi(p1);
        _y = std::stoi(p2);
        _op = content[left + 1];

        return true;

#else
        Json::Value root;
        Json::Reader r;
        r.parse(content, root);

        _x = root["x"].asInt();
        _op = root["op"].asInt();
        _y = root["y"].asInt();

        return true;

#endif
    }

    void Print()
    {
        std::cout << _x  << " " << _op << " " << _y << std::endl;
    }

public:
    int _x;
    int _y;
    char _op; // + - * / %
};

class Respone
{
public:
    Respone(int result = 0,int code = 0)
        : _result(result)
        , _code(code)
    {

    }

    bool serialize(std::string& content)
    {
        // "a + b" 
        // 构建报文的有效载荷
#ifdef Myself
        content = std::to_string(_result); 
        content += num_sep;
        content += std::to_string(_code);
        return true;

#else
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;

        Json::FastWriter w;
        content = w.write(root);

        return true;
#endif
    }

    bool deserialize(std::string& content)
    {
#ifdef Myself
        size_t pos = content.find(num_sep);
        if(pos == std::string::npos)
            return false;

        std::string part1 = content.substr(0, pos);
        std::string part2 = content.substr(pos + 1);

        _result = std::stoi(part1);
        _code = std::stoi(part2);

        return true;

#else
        Json::Value root;
        Json::Reader r;
        r.parse(content, root);

        _result = root["result"].asInt();
        _code = root["code"].asInt();

        return true;

#endif
    }

    void Print()
    {
        std::cout << "任务响应完成 result: " << _result << " exitcode: " << _code << std::endl; 
    }

public:
    int _result;
    int _code;
};
